/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ytmc.videoManager;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YtStatistics;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import ytmc.uploader.Authentication;
import ytmc.uploader.Credentials;

/**
 *
 * @author Tadeusz
 */
public class AccountDataRetriever {
    
    private ArrayList<AccountData> videos;
    
    private final String clientID = "YouTube Management Center";
    private final String developersKey = "AI39si5vkI-xc6wsH6vV5ZuY3ez0OGfQ5C0UOEnwCKnQlzPWsVFzVBVS9VPvEKlsNe_JE7JN89u-fZ9_jU1MDztMtSt-DcOHEA";
    private YouTubeService service;
    private String userUploadedVideosFeedURL;
    private VideoFeed videoFeed;
    private Authentication auth;
    
    public AccountDataRetriever()
    {
        videos = new ArrayList<AccountData>();
        service = new YouTubeService(clientID, developersKey);
        auth = new Authentication(service);
    }
    
    public void getAccountData()
    {
        try
        {
            Credentials credentials = new Credentials();
            auth.Authenticate(credentials);
            userUploadedVideosFeedURL = "http://gdata.youtube.com/feeds/api/users/" + credentials.getLogin() + "/uploads";
            videoFeed = service.getFeed(new URL(userUploadedVideosFeedURL), VideoFeed.class);
            String count = null;
            for (VideoEntry videoEntry : videoFeed.getEntries())
            {
                YtStatistics stats = videoEntry.getStatistics();
                if (stats != null)
                {
                    count = Long.toString(stats.getViewCount());
                } else
                {
                    count = "0";
                }
                videos.add(new AccountData(videoEntry.getTitle().getPlainText(), videoEntry.getMediaGroup().getYouTubeCategory().getContent(), videoEntry.getMediaGroup().getDescription().getPlainTextContent(), count, videoEntry.getMediaGroup().getVideoId()));
            }
        }
        catch (Exception ex)
        {
            Logger.getLogger(AccountDataRetriever.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
    public void removeVideo(AccountDataRetriever dataRetriever, int row) throws IOException, ServiceException
    {
        try
        {
        
            Credentials credentials = new Credentials();
            auth.Authenticate(credentials);

            userUploadedVideosFeedURL = "http://gdata.youtube.com/feeds/api/users/" + credentials.getLogin() + "/uploads";
            videoFeed = service.getFeed(new URL(userUploadedVideosFeedURL), VideoFeed.class);

            for (VideoEntry videoEntry : videoFeed.getEntries())
            {
                if (videoEntry.getMediaGroup().getVideoId().trim().equals(dataRetriever.getVideos().get(row).getVideoID().trim()))
                {
                    videoEntry.delete();
                }
            }
        } 
        catch (Exception ex)
        {
            Logger.getLogger(AccountDataRetriever.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void simulateAccountDataRetriever()
    {
        for(int i = 0; i < 5; i++)
        {
            videos.add(new AccountData("cygan", "śmieszne", "cygan z Torunia","5","idmgjgghhj"));
        }        
    }
    
    public ArrayList<AccountData> getVideos()
    {
        return videos;
    }
    
}
